<template>
  <!-- 侧边栏容器，宽度根据折叠状态调整 -->
  <el-aside :width="width" class="left-aside">
    <!-- 导航菜单，支持折叠 -->
    <el-menu :collapse="isCollapse" :collapse-transition="false">
      <!-- 带图标的菜单标题 -->
      <h3 class="menu-header">
        <img src="@/assets/icons/jianzhang.svg" alt="Logo" class="menu-icon" />
        <span class="menu-title">后台管理系统</span>
      </h3>

      <!-- 无子菜单的菜单项 -->
      <el-menu-item v-for="item in noChildren" :index="item.path" :key="item.path">
        <!-- 菜单图标 -->
        <component class="icons" :is="item.icon" />
        <!-- 菜单文本 -->
        <span>{{ item.label }}</span>
      </el-menu-item>

      <!-- 有子菜单的菜单项 -->
      <el-sub-menu v-for="item in hasChildren" :index="item.path" :key="item.path">
        <!-- 父级菜单 -->
        <template #title>
          <!-- 父级菜单图标 -->
          <component class="icons" :is="item.icon" />
          <!-- 父级菜单文本 -->
          <span>{{ item.label }}</span>
        </template>

        <!-- 子菜单项 -->
        <el-menu-item v-for="(subItem, subIndex) in item.children" :index="subItem.path" :key="subItem.path">
          <!-- 子菜单图标 -->
          <component class="icons" :is="subItem.icon" />
          <!-- 子菜单文本 -->
          <span>{{ subItem.label }}</span>
        </el-menu-item>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script lang="ts" setup>
import { ref, computed } from "vue";
import { useRouter } from "vue-router";
import { useAllDataStore } from '@/stores'
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from "@element-plus/icons-vue";
import component from "element-plus/es/components/tree-select/src/tree-select-option.mjs";

const router = useRouter();

const stores = useAllDataStore();

// 从store中获取菜单折叠状态
const isCollapse = computed(() => stores.state.isCollapse);

// 根据折叠状态计算侧边栏宽度
const width = computed(() => (isCollapse.value ? "64px" : "200px"));

// 菜单数据
const list = ref([
  {
    path: "/home",
    name: "home",
    label: "首页",
    icon: "house",
    url: "home",
  },
  {
    path: "/mall",
    name: "mall",
    label: "商品管理",
    icon: "video-play",
    url: "mall",
  },
  {
    path: "/user",
    name: "user",
    label: "用户管理",
    icon: "user",
    url: "user",
  },
  {
    path: "/other",
    label: "其他",
    icon: "location",
    children: [
      {
        path: "/page1",
        name: "page1",
        label: "页面1",
        icon: "setting",
        url: "page1",
      },
      {
        path: "/page2",
        name: "page2",
        label: "页面2",
        icon: "setting",
        url: "page2",
      },
    ],
  },
]);

// 筛选出无子菜单的项
const noChildren = computed(() => list.value.filter((item) => !item.children));

// 筛选出有子菜单的项
const hasChildren = computed(() => list.value.filter((item) => item.children));

// 菜单点击事件
const clickMenu = (item: any) => {
  router.push(item.path);
};

// 菜单展开事件
const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
};

// 菜单关闭事件
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath);
};
</script>

<style scoped>
.icons {
  width: 18px;
  height: 18px;
  font-size: 16px;
  margin-right: 5px;
  /* 使用滤镜将图标改为白色 */
  filter: brightness(0) invert(1);
}

.menu-header {
  display: flex;
  align-items: center;
  padding: 0 20px;
}

.menu-icon {
  width: 20px;
  height: 20px;
  margin-right: 10px;
  /* 使用滤镜将图标改为白色 */
  filter: brightness(0) invert(1);
}

.menu-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.el-menu {
  border: none;
  background-color: #1F2B38;
  color: #BDC3C7;

  h3 {
    line-height: 48px;
    color: #fff;
    margin: 0;
  }
}

/* 设置默认状态下的菜单项样式 */
.el-menu-item,
.el-sub-menu__title {
  background-color: #1F2B38 !important;
  color: #BDC3C7 !important;
}

/* 修复子菜单标题悬停时的文字颜色 */
.el-sub-menu__title:hover span {
  color: #BDC3C7 !important;
}

/* 修复子菜单标题悬停时的图标颜色 */
.el-sub-menu__title:hover .icons {
  filter: brightness(0) invert(0.8) !important; /* 保持灰色，不要变纯白 */
}

/* 设置菜单项悬停样式 */
.el-menu-item:hover,
.el-sub-menu__title:hover {
  background-color: #34495E !important;
}

/* 设置菜单项激活状态样式 */
.el-menu-item.is-active {
  background-color: #34495E !important;
  color: #fff !important;
}

/* 设置子菜单项默认文字颜色 */
.el-sub-menu__title span {
  color: #BDC3C7 !important;
}

/* 确保子菜单展开时标题样式 */
.el-sub-menu.is-opened > .el-sub-menu__title {
  background-color: #1F2B38 !important;
}

/* 子菜单展开时标题悬停样式 */
.el-sub-menu.is-opened > .el-sub-menu__title:hover {
  background-color: #34495E !important;
}

/* 修复子菜单悬停时的背景色 */
:deep(.el-sub-menu__title:hover) {
  background-color: #34495E !important;
}

/* 修复子菜单展开时的背景色 */
:deep(.el-sub-menu.is-opened .el-sub-menu__title) {
  background-color: #1F2B38 !important;
}

/* 修复子菜单展开时悬停的背景色 */
:deep(.el-sub-menu.is-opened .el-sub-menu__title:hover) {
  background-color: #34495E !important;
}

.el-aside {
  height: 100%;
  background-color: #1F2B38;
}

.left-aside {
  border-color: hsl(var(--border));
  box-sizing: border-box;
  border-style: solid;
  border-width: 2px;
}
</style>